/*
 *用于计算藏品盲盒的发售状态既倒计时计算
 *
 */

import mixin, {
	data
} from "uview-ui/libs/mixin/mixin";


import {mapState,mapActions} from 'vuex'
//mix中混入的data的键必须加特定前缀，当前加mixColl
export default {
	data() {
		return {
			
		}
	},
	computed: {
		saleStatus() { //发售状态  1未开始，2已开始，3已结束，4已售完
			if (!this.product.id) return 3;
			
			let timeStatus = this.$tools.checkTimeBetween(this.nowTimestamp, this.saleStartTime, this.saleEndTime);
			if (timeStatus == 2) { //已结束(显示已售罄)
				clearInterval(this.mixCollTimer);
				return 4  //本应该显示3
			}
			//售卖中，未开始
			if (!timeStatus) return 1; //未开始
			if (this.saleTotalNum >= this.totalNum) return 4; //已售完
			return 2;
		},
		saleStartTime() { //售卖开始时间
			if (!this.product.id) return new Date().getTime()
			if (this.product.goods_type == 1) return this.product.release_time * 1000
			if (this.product.batch) return this.product.batch.start_time * 1000;
			return this.product.release_time * 1000;
		},
		saleEndTime() { //售卖结束时间
			if (!this.product.id) return new Date().getTime()


			//盲盒没有结束时间
			if (this.product.goods_type == 1) return new Date().getTime()+1000 * 60 * 60 * 24;
			if (this.product.batch) return this.product.batch.end_time * 1000;
			return this.product.end_time * 1000;
		},
		totalNum() { //发行量
			if (!this.product.id) return 0
			if (this.product.goods_type == 1) return this.product.total_num
			if (this.product.batch) return this.product.batch.total_num;
			return this.product.total_num;
		},
		saleTotalNum() { //已售数量
			if (!this.product.id) return 0

			if (this.product.goods_type == 1) return this.product.sale_total_num
			//batch  里面has_num是库存
			if (this.product.batch) return this.totalNum-this.product.batch.has_num;
			return this.product.sale_total_num;
		},
		//当前批次的库存
		inventory() {
			if (this.product.goods_type == 1) {
				let batchInv = this.product.total_num - this.product.sale_total_num
				return batchInv > 0 ? batchInv : 0
			} else {
				//藏品取批次的库存
				return  this.product.batch.has_num
			}
		},
		countDown() {
			if (this.saleStatus == 1) { //未开始倒计时
				return this.saleStartTime - this.nowTimestamp;
			}
			return 0;
		},
		//最大购买数
		maxBuyCount(){
			return this.product.purchasing_num
		},
		...mapState('config',['nowTimestamp']),

	},
	mounted(){
		this.createNowTimeTimer();
	},
	onLoad(){
	},
	methods: {
		...mapActions('config',['createNowTimeTimer'])	
	}
}
